<?php
namespace app\index\controller;
use app\index\model\Blog;
use app\index\model\Comment;
use app\index\model\Tag;
use think\Controller;
use think\View;
use app\index\controller\Index;
use think\Db;
use app\extra\weibo\SaeTOAuthV2;
use app\index\common\OAuthcommon;
class Article extends Controller
{
    public function read($id='')
    {
        $article=Blog::get($id);
        $view = new View();
        $view->username=session('username');
        $view->assign('article',$article);
        $top = $this->displaytop();
        $view->assign('top',$top);

        $category=Db::name('category')->where('display',1)->select();
        $view->assign('category',$category);

        $tag = new Tag();
        $taglist = $tag->select();
        $view->assign('taglist',$taglist);

        $cat=Db::name('category')->where('cid',$article['cid'])->find();
        $view->assign('cat',$cat);

        $tag1 = $tag->where('tid',$article['tid'])->find();
        $view->assign('tag1',$tag1);

        //微博登录开始
        $o = new OAuthcommon();
        $code_url = $o->Weibourl();
        $view->assign('code_url',$code_url);

        //微博信息读取开始
        $user_message = $o->Weiboinfo();
        $view->assign('user_message',$user_message);
        //微博信息读取结束

        $c = new Comment();
        $comment = $c->getCommentData($id);
        $view->assign('comment',$comment);



        return $view->fetch('index');
    }

    public function displaytop()
    {
        $blog = new Blog();
        $top = $blog
            ->where('is_top',1)
            ->select();
        return $top;
    }


    public function ajaxReturn(){

        $art_id = input('post.art_id');
        $content = input('post.comment');
        $user_id =  input('post.user_id');
        $screen_name = input('post.screen_name');
        $profile_image_url = input('post.profile_image_url');
        $comment = new Comment;
        $comment->art_id = $art_id;
        $comment->content = $content;
        $comment->p_id = 1;
        $comment->user_id =  $user_id;
        $comment->screen_name = $screen_name;
        $result = $comment->save();
        if($result){
            $array = array("info"=>"评论成功",
                            "status"=>1,
                            "user_id"=>$user_id,
                            "content" =>$content,
                            "date"=>date('Y-m-d H:i:s'),
                            "screen_name"=>$screen_name,
                            "profile_image_url"=>$profile_image_url
            );
        }else{
            $array = array("info"=>"评论失败");
        }
        echo json_encode($array);

    }
}